package com.lsz.intelligentflowerwatering.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Wifi
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.lsz.intelligentflowerwatering.ui.theme.MyComposeTheme
import com.lsz.intelligentflowerwatering.viewmodel.WifiViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.espressif.provisioning.ESPConstants
import com.lsz.intelligentflowerwatering.viewmodel.MainViewModel

@Composable
fun PasswordDialog(
    showDialog: Boolean,
    onDismiss: () -> Unit,
    onConfirm: (String) -> Unit
) {
    if (showDialog) {
        var password by remember { mutableStateOf("") }
        AlertDialog(
            onDismissRequest = onDismiss,
            title = { Text(text = "Enter Password") },
            text = {
                TextField(
                    value = password,
                    onValueChange = { password = it },
                    label = { Text("Password") },
                    visualTransformation = PasswordVisualTransformation(),
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
                )
            },
            confirmButton = {
                Button(onClick = { onConfirm(password) }) {
                    Text(text = "OK")
                }
            },
            dismissButton = {
                Button(onClick = onDismiss) {
                    Text(text = "Cancel")
                }
            }
        )
    }
}

@Composable
fun ConfigProgressDialog(
    progress: Float,
    state: MutableState<Boolean> = mutableStateOf(true)
) {
    if (progress > 0)
        Dialog(onDismissRequest = { }) {
            CircularProgressIndicator(progress = progress)
        }

}

@Composable
fun WifiConfig(
    wifiViewModel: WifiViewModel = viewModel()
) {
    val dialogState = remember { mutableStateOf(false) }
    var selectedWifiSsid = remember { "" }

    PasswordDialog(dialogState.value, { dialogState.value = false }, { pwd ->
        dialogState.value = false
        wifiViewModel.wifiConnect(selectedWifiSsid, pwd)
    })
    ConfigProgressDialog(progress = wifiViewModel.progress.value)
    Column(Modifier.padding(30.dp)) {
        TheText(text = "可用网络")
        LazyColumn(
            modifier = Modifier
                .fillMaxWidth()
                .weight(4f)
                .background(MyComposeTheme.colors.listBackground, shape = RoundedCornerShape(20.dp))
                .padding(30.dp)
        ) {
            itemsIndexed(wifiViewModel.wifiDeviceList) { index, item ->
                Row(
                    Modifier
                        .fillMaxWidth()
                        .height(60.dp)
                        .padding(start = 10.dp)
                        .clickable { selectedWifiSsid = item.wifiName },
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        item.wifiName,
                        Modifier
                            .padding(start = 10.dp)
                            .fillMaxWidth()
                    )
                    if (item.security.toShort() != ESPConstants.WIFI_OPEN) {
                        Icon(
                            imageVector = Icons.Filled.Lock,
                            contentDescription = "WIFI",
                            modifier = Modifier
                                .align(
                                    Alignment.CenterVertically
                                )
                                .size(30.dp)
                                .padding(end = 10.dp)
                        )
                    }
                    Icon(
                        imageVector = Icons.Filled.Wifi,
                        contentDescription = "WIFI",
                        modifier = Modifier
                            .align(
                                Alignment.CenterVertically
                            )
                            .size(30.dp)
                            .padding(end = 10.dp)
                    )
                }
                if (index < wifiViewModel.wifiDeviceList.lastIndex) {
                    Divider()
                }
            }
        }
        Row(modifier = Modifier.weight(1f)) {

        }
    }
}